package ch.fhnw.qualiscan.application.controllers;

import java.util.ArrayList;
import java.util.Set;

import ch.fhnw.qualiscan.application.elements.EvaluationDomain;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.RoleModel;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.application.views.EvaluationView;
import ch.fhnw.qualiscan.core.Controller;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Role;
import ch.fhnw.qualiscan.core.db.pojos.RoleText;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;
import ch.fhnw.qualiscan.core.db.pojos.User;
import ch.fhnw.qualiscan.core.helpers.Filter;
import ch.fhnw.qualiscan.core.helpers.Permissions;
import ch.fhnw.qualiscan.core.helpers.StageHelper;
import ch.fhnw.qualiscan.core.libraries.NotificationLibrary;
import eu.livotov.tpt.i18n.TM;

public class Evaluation extends Controller {
	
	public Survey currentSurvey;
	public SurveyUserRole sur;
	public int currentRole;
	
	public SurveyModel surveyModel;
	public DomainModel domainModel;
	public UserModel userModel;
	public RoleModel roleModel;
	public StageHelper stageHelper;
	private EvaluationView evalView;
	
	private User user;
	int currentFilter;
	Filter filter;
	ArrayList<EvaluationDomain> allDomains = new ArrayList<EvaluationDomain>();
	
	public ArrayList<RoleText> getRoleTexts() {

		ArrayList<Role> roles = roleModel.getRoles(currentSurvey);
		ArrayList<RoleText> roleTexts = new ArrayList<RoleText>();
		for (int r = 0; r  < roles.size(); r++) {
			roleTexts.add(userModel.getRoleText(roles.get(r), userSession.getLanguage()));
		}
		
		return roleTexts;
	}
	
	public void update(Filter filter) {
		for(EvaluationDomain d : allDomains) {
			d.update(filter);
		}
	}
	
	
	@Override
	public void init() {
		filter = new Filter();
		user = new User();
		user.setId(userSession.getId());
	}

	@Override
	public void setView() {
		view = new EvaluationView(this);
		evalView = (EvaluationView) view;
		evalView.buildLayout();
		
	}

	@Override
	public void loadModels() {
		surveyModel = new SurveyModel();
		domainModel = new DomainModel();
		userModel = new UserModel();
		stageHelper = new StageHelper();
		roleModel = new RoleModel();
		
	}

	@Override
	public void setNeededPermissions() {
		neededPermissions.add(Permissions.LOGGEDIN);
		
	}
	
	/**
	 * This function is responsible for loading the content
	 * @return status
	 */
	public boolean loadContent() {
		// load content (role and survey determined)
		if(currentRole >= 0 && currentSurvey != null) {
			
			evalView.charts.removeAllComponents();
			
			// loading domains
			Set<Domain> domains = currentSurvey.getDomains();
			
			for(Domain d : domains) {
				EvaluationDomain domainElement = new EvaluationDomain(this, d, filter);
				evalView.charts.addComponent(domainElement);
				allDomains.add(domainElement);
			}
			
			return true;
		} 
		
		// not enough data to load content
		else {
			return false;
		}
	}
	
	@Override
	public void urlFragmentChanged(int fragmentId, String fragment) {
		
		if (fragmentId == 2) {
			
			int fragmentPar = Integer.parseInt(fragment);
			
			Role roleCheck = roleModel.getRoleById(fragmentPar);
			if(roleCheck != null) {
				currentRole = roleCheck.getId();
				evalView.changeFilter(currentRole);
				filter.setRoleId(currentRole);
				update(filter);
			} else if(fragmentPar == 0) {
				currentRole = 0;
				evalView.changeFilter(currentRole);
				filter.setRoleId(currentRole);
				update(filter);
			}
			else {
				currentRole = -1;
				evalView.unload();
				NotificationLibrary.showError(TM.get("error"), TM.get("evaluation.error.second.parameter"));
			}
		}
		
		if (fragmentId == 3) {
			
			try {
				int surId = Integer.parseInt(fragment);
				
				sur = surveyModel.getSurById(surId);
				currentSurvey = sur.getSurvey();
				filter.setSurveyId(currentSurvey.getId());
				filter.setSurId(surId);
				// check if survey exists and whether user has access
				if (sur.getUser().getId() != user.getId()) {
					//urlLib.setPage("PermissionDenied");
				}
					
				if(currentSurvey != null /*&& thisSurvey.getStage().getId() == Config.STAGE_EVALUATION*/) {
					evalView.populatePage();
					loadContent();
				} else {
					evalView.unload();
					NotificationLibrary.showError(TM.get("error"), TM.get("evaluation.error.access"));
				}
				
			} catch(NumberFormatException e) {
				NotificationLibrary.showError(TM.get("error"), TM.get("error.parameter.invalid"));
			}
		}
		
	}

	
}
